package com.jamcodingnm.apps.abqevents.rss;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jamcodingnm.apps.abqevents.RSSUtility;

/**
 * Object representation of item data from RSS Feed.
 * 
 * @author Marcos
 * 
 */
public class RSSItem {
	private String title = null;
	private String description = null;
	private String link = null;
	private String category = null;
	private String stringDate = null;
	private Date pubDate = null;
	private String month = null;
	private Integer day = null;
	private Integer year = null;
	private Long pubTime = null;

	private List<String> detailsList = new ArrayList<String>();
	private String details = null;
	private String imageURL = null;

	/**
	 * Blank constructor.
	 */
	protected RSSItem() {
	}

	/**
	 * @return month 3 letter representation
	 */
	protected String getMonth() {
		if (pubDate == null)
			return "";
		else
			return month;
	}

	/**
	 * @return the title from the current item in RSS feed
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @return the description from current item in the RSS feed
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return the link from the current item in the RSS feed
	 */
	public String getLink() {
		return link;
	}

	/**
	 * @return the category from the current item in the RSS feed
	 */
	protected String getCategory() {
		return category;
	}

	/**
	 * @return the image from the current item in the RSS feed
	 */
	protected String getImageURL() {
		return imageURL;
	}

	/**
	 * @return the day of the month 1-31 from the pubDate
	 */
	protected Integer getDay() {
		if (pubDate == null)
			return 0;
		else
			return day;
	}

	/**
	 * @return the year of the pubDate
	 */
	protected Integer getYear() {
		if (pubDate == null)
			return 0;
		else
			return year;
	}

	/**
	 * @return the time from the pubDate
	 */
	protected Long getPubTime() {
		return pubTime;
	}

	/**
	 * @return the string date from the RSS feed
	 */
	protected String getStringDate() {
		return stringDate;
	}

	/**
	 * @return the date parsed from RSS feed
	 */
	protected Date getPubDate() {
		return pubDate;
	}

	/**
	 * @return List of details about the event
	 */
	protected List<String> getDetail() {
		return detailsList;
	}

	/**
	 * Sets the RSS stringDate, pubDate, month
	 * 
	 * @param stringDate
	 */
	@SuppressWarnings("deprecation")
	protected void setStringDate(String stringDate) {
		setPubDate(RSSUtility.stringToDate(stringDate));
		if (pubDate != null) {
			setMonth(new SimpleDateFormat("MMM").format(getPubDate()));
			setDay(getPubDate().getDay());
			setYear(getPubDate().getYear() + 1900);
			setPubTime(getPubDate().getTime());
		}
		this.stringDate = stringDate;
	}

	protected void setMonth(String month) {
		this.month = month;
	}

	protected void setDay(Integer day) {
		this.day = day;
	}

	protected void setYear(Integer year) {
		this.year = year;
	}

	protected void setPubTime(Long pubTime) {
		this.pubTime = pubTime;
	}

	protected void setPubDate(Date pubDate) {
		this.pubDate = pubDate;
	}

	protected void addDetail(String detail) {
		this.detailsList.add(detail);
	}

	protected void setDetail(List<String> detail) {
		this.detailsList = detail;
	}

	public String getDetails() {
		if (detailsList == null)
			return "";

		int detailListSize = detailsList.size();
		int detailLineNo = 1;
		
		setDetails(null);
		
		for (String detail : detailsList) 
		{
			if (details == null) 
			{
				if (detailLineNo == detailListSize)
					details = detail;
				else
					details = detail + "\n";
			} 
			else if (detailLineNo != detailListSize) 
			{
				details = details + detail + "\n";
			} 
			else
			{
				details = details + detail;
			}
			detailLineNo++;
		}

		if (details == null)
			return "";
		else
			return details;
	}

	protected void setDetails(String details) {
		this.details = details;
	}

	protected void setTitle(String title) {
		this.title = title;
	}

	protected void setDescription(String description) {
		this.description = description;
	}

	protected void setLink(String link) {
		this.link = link;
	}

	protected void setCategory(String category) {
		this.category = category;
	}

	protected void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		// limit how much text you display
		if (title.length() > 42) {
			return title.substring(0, 42) + "...";
		}
		return title;
	}
}